package cn.lyjuan.android.demofive.frame.realm;

import cn.lyjuan.android.mylibrary.domain.IdModel;
import io.realm.Realm;
import io.realm.RealmModel;
import io.realm.RealmObject;
import io.realm.RealmResults;
import io.realm.annotations.Ignore;
import io.realm.annotations.Index;
import io.realm.annotations.PrimaryKey;
import io.realm.annotations.RealmClass;
import io.realm.annotations.Required;

/**
 * Created by chad on 05/11/2017.
 */
@RealmClass
public class UserModel implements RealmModel
{
    @PrimaryKey
    private Integer id;

    @Required
    @Index
    private String name;

    private Integer age;

    @Ignore
    private IdModel idModel;

    /**
     * 增加用户
     *
     * @param realm
     * @param name
     * @param age
     * @return
     */
    public static UserModel add(Realm realm, String name, Integer age)
    {
        UserModel user = new UserModel(realm, name, age);

        realm.beginTransaction();
        realm.copyToRealmOrUpdate(user);
        realm.commitTransaction();

        return user;
    }

    public void del()
    {
        Realm realm = RealmObject.getRealm(this);
        realm.beginTransaction();
        RealmObject.deleteFromRealm(this);
        realm.commitTransaction();
    }

    /**
     * 根据名称查找
     *
     * @param realm
     * @param name
     * @return
     */
    public static RealmResults<UserModel> findByName(Realm realm, String name)
    {
        return realm.where(UserModel.class).like("name", "%" + name + "%").findAll();
    }

    /**
     * 查找所有
     *
     * @param realm
     * @return
     */
    public static RealmResults<UserModel> findAll(Realm realm)
    {
        return realm.where(UserModel.class).findAll();
    }

    /**
     * 根据id查找
     *
     * @param realm
     * @param id
     * @return
     */
    public static UserModel findById(Realm realm, Integer id)
    {
        UserModel user = realm.where(UserModel.class).equalTo("id", id).findFirst();

        return user;
    }

    private UserModel(Realm realm, String name, Integer age)
    {
        this.idModel = IdModel.fromRealm(realm, UserModel.class);
        this.id = this.idModel.increment();// ID增一

        this.name = name;
        this.age = age;
    }

    /**
     * 从普通对象转为Realm管理的对象
     *
     * @param realm
     * @return
     */
    public UserModel toRealm(Realm realm)
    {
        if (RealmObject.isManaged(this))
            return this;
        return realm.copyToRealm(this);
    }

    /**
     * 从Realm管理的对象转为普通对象
     *
     * @param realm
     * @return
     */
    public UserModel fromRealm(Realm realm)
    {
        if (!RealmObject.isManaged(this))
            return this;

        return realm.copyFromRealm(this);
    }

    public UserModel()
    {
    }

    public Integer getId()
    {
        return id;
    }

    /**
     * 非内部不能改变ID
     *
     * @param id
     */
    private void setId(Integer id)
    {
        this.id = id;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public Integer getAge()
    {
        return age;
    }

    public void setAge(Integer age)
    {
        this.age = age;
    }

    @Override
    public String toString()
    {
        return "UserModel{" +
                "id=" + id +
                ", name='" + name + '\'' +
                ", age=" + age +
                ", idModel=" + idModel +
                '}';
    }
}
